package com.Action;

import gongju.FormatUtils;
import gongju.RedisUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import gongju.JSONUtils;

import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.w3c.tidy.Out;

import redis.clients.jedis.Jedis;

import com.Bean.createRoomUtilModel;
import com.Bean.wdhtbuildModel;
import com.Bean.wdhtprojectModel;
import com.Bean.wdhtroomModel;
import com.Service.wdhtbuildService;
import com.Service.wdhtprojectService;
import com.Service.wdhtroommodelService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.mysql.fabric.xmlrpc.base.Array;

@Controller
@RequestMapping("/htbuild")
public class wdhtroommodelAction {
	@Autowired
	private wdhtroommodelService service;
	@Autowired
	private wdhtbuildService services;
	@Autowired
	private wdhtprojectService projectservice;
	private Thread tt;
	@RequestMapping("/goaddhtroom.do")
	public String goaddroom(Model model,wdhtbuildModel beans){
		System.out.println(beans.getBuildId());
		wdhtbuildModel bean = services.selectByPrimaryId(beans.getBuildId());
		model.addAttribute("bean",bean);		
		return "createroom";	
	}
	@RequestMapping("/addRoomredis.do")
	public void roomredis(HttpServletResponse response,@RequestBody final wdhtroomModel[] arr) throws IOException{
		final Jedis jedis=RedisUtil.getJedis();
//		final Map<String,String> map=new HashMap<String,String>();
//		for(wdhtroomModel cru:arr){
//			UUID uuid=UUID.randomUUID();
//			cru.setRoomId(uuid+"");
//		}
//		final wdhtroomModel[] rediss = arr;
			Long llen = jedis.llen("rooms");
	        System.out.println("----------------->"+llen);
	    	if(llen>0){
	    		jedis.flushAll();
	    		}
	    	/**
		     * Redis线程
		     */
	    	tt = new Thread("Redis"){
	            public void run(){
	            	for(wdhtroomModel cru:arr){
	            		Map<String,String> map=new HashMap<String,String>();
	        			map.put("apartmentLayoutId", cru.getApartmentLayoutId());
	        			map.put("unitId", cru.getUnitId());
	        			map.put("floor"+"", cru.getFloor()+"");
	        			map.put("roomCode", cru.getRoomCode());
	        			map.put("projectId", cru.getProjectId());
	        			map.put("buildId", cru.getBuildId());
	        			map.put("state", cru.getState()+"");
	        			map.put("buildUpArea", cru.getBuildUpArea()+"");
	        			map.put("innerArea", cru.getInnerArea()+"");
	        			map.put("innerPrice", cru.getInnerPrice()+"");
	        			map.put("unitPrice", cru.getUnitPrice()+"");
	        			map.put("purposeId", cru.getPurposeId());
	        			map.put("deleteState", cru.getDeleteState());
	        			map.put("rommId", cru.getRoomId());
	        			
	            	String string =JSONObject.fromObject(map).toString();
	    			
	            	jedis.lpush("rooms", string);
	            	}
	    			RedisUtil.returnBrokenResource(jedis);
	            } 
	        };
	        tt.start();
	    /**
	     * MySQL线程
	     */
		new Thread("MySQL"){
            public void run(){
            	service.insert(arr);
            	}
            }.start();
        response.getWriter().print("");
	}
	@RequestMapping("/listhtroom.do")
	public String selectall(Model model){
		List<wdhtroomModel> selectall = service.selectall();
		model.addAttribute("list",selectall);
		return "Wdroom";
	}	
    @RequestMapping("/listhtroomredis.do")
    public String listhtroomredis(Model model) throws JsonParseException, JsonMappingException, InstantiationException, IllegalAccessException, IOException{
    	if(!tt.isAlive()){
    		System.out.println(222);
    		Jedis jedis=RedisUtil.getJedis();
    		List<String> lrange = jedis.lrange("rooms", 0, -1);
	    	List<wdhtroomModel> list = new ArrayList<wdhtroomModel>();
	    	for(String str:lrange){
	    		wdhtroomModel room =null;
	    		room = FormatUtils.jsonToObject(str, wdhtroomModel.class);
	    		list.add(room);
	    	}
	    	model.addAttribute("list", list);
	    	return "Wdroom";
		}
    	return "redirect:wait.do";
	}
    @RequestMapping("/wait.do")
	public String wait(Model model){
    	System.out.println("111111");
		  return "redirect:listhtroomredis.do";
	}
	@RequestMapping("/goaddhtroomonly.do")
	public String goaddroom(Model model){
		  List<wdhtprojectModel> selectall = projectservice.selectall();
		  model.addAttribute("list",selectall);
		  return "Addhtroom";
	}
	
	@RequestMapping("/addRoomajax.do")
	public void ajax(PrintWriter out,String projectId) throws IOException{
		System.out.println(projectId);
		List<wdhtbuildModel> selectall = services.selectprojectroom(projectId);
		String str="";
		int flag=0;
        for(wdhtbuildModel a:selectall){
        	if(flag==0){
        	str=a.getBuildName();
        	}
        	if(flag==1){
            str=str+","+a.getBuildName();
        	}
        	flag=1;
        }	
		out.write(str);
	}
	@RequestMapping("/addRoomajaxs.do")
	public void ajaxs(PrintWriter out,String projectId,String buildName) throws IOException{
		System.out.println(projectId);
		wdhtbuildModel bean =new wdhtbuildModel();
		bean.setBuildName(buildName);
		bean.setProjectId(projectId);
		wdhtbuildModel selectbuildroom = services.selectbuildroom(bean);
		String str=selectbuildroom.getRoomCount()+"";
		out.write(str);
	}
	
	@RequestMapping("/addhtroom.do")
	public String addhtroom(wdhtroomModel bean){
		System.out.println(133);
		service.insertSelective(bean);
		return "redirect:listhtroom.do";
	}
	
		@RequestMapping("/deletehtroom.do")
		public String deletehtroom(wdhtroomModel bean){
			service.deleteByPrimaryKey(bean.getRoomId());			
			return "redirect:listhtroom.do";
		}
		
		@RequestMapping("/goupdatehtroom.do")
	    public String goupdatehtroom(Model model,wdhtroomModel bean){
			System.out.println("dddddddd");
			/*List<wdhtprojectModel> selectall = projectservice.selectall();
			model.addAttribute("list",selectall);
			System.out.println("陈浩"+bean.getRoomId());
			wdhtroomModel selectByPrimaryKey = service.selectByPrimaryKey(bean.getRoomId());
			System.out.println(selectByPrimaryKey.getProjectId());
			model.addAttribute("bean", selectByPrimaryKey);
			System.out.println(selectByPrimaryKey.getBuildId()+"12"+selectByPrimaryKey.getRoomCode());
			System.out.println(selectByPrimaryKey.getRoomId());*/
			return "Updatehtroom";
		}
		
			@RequestMapping("/updatehtroom.do")
			public String updatehtbuild(wdhtroomModel bean){
				System.out.println(bean.getBuildUpArea());
				service.updateByPrimaryKeySelective(bean);				
				return "redirect:listhtroom.do";
			}
				@RequestMapping("/selectlikeroom.do")
				public String selectlikerooom(Model model,wdhtroomModel bean){		
					wdhtroomModel record=new wdhtroomModel();
					record.setProjectId(bean.getProjectId());
					record.setBuildId(bean.getBuildId());
					record.setUnitId(bean.getUnitId());
					List<wdhtroomModel> selectlikeroom = service.selectlikeroom(record);
					model.addAttribute("list", selectlikeroom);
					return "Wdroom";
				}
}
